package com.yyj.shopOnLine.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yyj.shopOnLine.user.dao.IAAdminsDao;
import com.yyj.shopOnLine.user.dao.IARoleDao;
import com.yyj.shopOnLine.user.pojo.AAdmins;
import com.yyj.shopOnLine.user.pojo.ARole;
import com.yyj.shopOnLine.user.service.IUsersService;
import com.yyj.shoppingOnLine.common.util.ConstatFinalUtil;
import com.yyj.shoppingOnLine.common.util.PageInfoUtil;

@Service("usersService")
public class UsersService  implements IUsersService
{
	@Resource
	IAAdminsDao adminsDao;
	@Resource
	IARoleDao roleDao;
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		JSONObject resultJson=new JSONObject();
		int res=adminsDao.save(admins);
		if(res>0)
		{
			resultJson.put("code", "0");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("0"));
		}
		else
		{
			resultJson.put("code", "1");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("1"));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJson=new JSONObject();
		int res=adminsDao.update(admins);
		if(res>0)
		{
			resultJson.put("code", "0");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("0"));
		}
		else
		{
			resultJson.put("code", "2");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("2"));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		JSONObject resultJson=new JSONObject();
		int res=adminsDao.delete(admins);
		if(res>0)
		{
			resultJson.put("code", "0");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("0"));
		}
		else
		{
			resultJson.put("code", "3");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("3"));
		}
		return resultJson;
	}

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return adminsDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil page, Map<String, Object> condMap)
	{
		/*处理关键字*/
		if(condMap.get("keyword")!=null)
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		/*分页查询多条记录*/
		if(page !=null )
		{
			/*获取总记录数*/
			condMap.put("pageCount", "true");
			condMap.put("currStartRecord", page.getCurrStartRecord());
			condMap.put("pageSize", page.getPageSize());
			List<AAdmins> adminsCount=adminsDao.findCondList(condMap);
			if(adminsCount.size()>0)
			{
				page.setTotalRecord(adminsCount.get(0).getId());
			}
			/*获取分页记录*/
			condMap.put("pageCount", "false");
			List<AAdmins> adminsList=adminsDao.findCondList(condMap);
			return adminsList;
		}
		/*不分页查询多条记录*/
		return adminsDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveOneRoleService(ARole role)
	{
		JSONObject resultJson=new JSONObject();
		int res=roleDao.save(role);
		if(res>0)
		{
			resultJson.put("code", "0");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("0"));
		}
		else
		{
			resultJson.put("code", "1");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("1"));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJson=new JSONObject();
		int res=roleDao.update(role);
		if(res>0)
		{
			resultJson.put("code", "0");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("0"));
		}
		else
		{
			resultJson.put("code", "2");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("2"));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role)
	{
		JSONObject resultJson=new JSONObject();
		int res=roleDao.delete(role);
		if(res>0)
		{
			resultJson.put("code", "0");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("0"));
		}
		else
		{
			resultJson.put("code", "3");
			resultJson.put("info", ConstatFinalUtil.RESOURCE_INFO.get("3"));
		}
		return resultJson;
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		return roleDao.findOne(condMap);
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil page, Map<String, Object> condMap)
	{
		/*处理关键字*/
		if(condMap.get("keyword")!=null)
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		/*分页查询多条记录*/
		if(page !=null )
		{
			/*获取总记录数*/
			condMap.put("pageCount", "true");
			condMap.put("currStartRecord", page.getCurrStartRecord());
			condMap.put("pageSize", page.getPageSize());
			List<ARole> roleCount=roleDao.findCondList(condMap);
			if(roleCount.size()>0)
			{
				page.setTotalRecord(roleCount.get(0).getId());
			}
			/*获取分页记录*/
			condMap.put("pageCount", "false");
			List<ARole> roleList=roleDao.findCondList(condMap);
			return roleList;
		}
		/*不分页查询多条记录*/
		return roleDao.findCondList(condMap);
	}

	
}
